﻿module Sustainalytics.Portfolio {
    import Common = Sustainalytics.Common;
    import SearchPortfolio = Sustainalytics.Portfolio.PortfolioSearchController;

    // Portfolio List controller
    export class PortfolioAddController {

        public portfolio: PortfolioDto;
        public portfolioAddUpdate: PortfolioAddDto;
        public portfolioFilter: PortfolioFilterDto;
        public isShared: boolean;
        public portfolioSearch: string;
        private accountId: string;
        private userId: string;

        // Ctor.
        constructor(
            private $rootScope: ng.IScope,
            private $scope: ng.IScope,
            private $q: ng.IQService,
            private $location: ng.ILocationService,
            private PortfolioService: PortfolioService,
            private PortfolioCompanyService: PortfolioCompanyService,
            private notificationFactory: Common.INotificationFactory,
            private $modal: Common.IModal,
            private $modalInstance: Common.IModalInstance,
            private filterId: string,
            private companyId: string) {

            var ctx = this;
            ctx.isShared = false;
            ctx.portfolioAddUpdate = new PortfolioAddDto();
            ctx.portfolioFilter = new PortfolioFilterDto();
            ctx.portfolioSearch = '';

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.accountId = ctx.$rootScope['userInfo'].accountId;
            ctx.userId = ctx.$rootScope['userInfo'].userId;

            $scope['portfolioAdd'] = ctx;
        }

        public cancel = function () {
            this.$modalInstance.dismiss('cancel');
        };

        public selectPortfolioAdd(item) {
            var ctx = this;
            ctx.portfolioAddUpdate.portfolioName = item.name;
            ctx.portfolioAddUpdate.portfolioId = item.id;
            ctx.portfolioFilter.namePortfolio = item.name;
            ctx.portfolioFilter.portfolioId = item.id;

            ctx.$rootScope['screeningExportDetails'].SelectedPortfolio = item.name;
        }

        public addPortfolio(): ng.IPromise<{}> {
            var ctx = this;

            var def = this.$q.defer();
            var portfolioCountTemp : number;

            ctx.portfolioAddUpdate.filterId = ctx.filterId;
            ctx.portfolioAddUpdate.isValid = true;

            ctx.portfolioFilter.isMyPortfolio = true;
            ctx.portfolioFilter.namePortfolio = ctx.portfolioSearch;
            ctx.portfolioFilter.isNameEqual = true;
            ctx.portfolioAddUpdate.portfolioName = ctx.portfolioSearch;
            ctx.portfolioAddUpdate.sharingStatus = ctx.isShared == true ? 2 : 1;

            //get portfolios count
            var promiseCount: ng.IPromise<number> = ctx.PortfolioService.getPortfolioCount(ctx.portfolioFilter, ctx.accountId, ctx.userId);

            //on success 
            promiseCount.then(function (count) {

                portfolioCountTemp = count['portfolioCount'];
                if (portfolioCountTemp == 0) {
                    ctx.$rootScope['loading'] = true;
                    var promiseAddPortfolio: ng.IPromise<{}> = ctx.PortfolioService.addPortfolios(ctx.portfolioAddUpdate);

                    //on success 
                    promiseAddPortfolio.then(function () {
                        ctx.$rootScope['screeningExportDetails'].SelectedPortfolio = ctx.portfolioAddUpdate.portfolioName;
                        ctx.$rootScope['loading'] = false;
                        ctx.cancel();
                    });
                }
                else if (portfolioCountTemp == 1) {
                    var modalInstancePortfolio = ctx.$modal.open({
                        templateUrl: '/Views/Portfolio/portfolioExists_dialog.tpl.html',
                        controller: 'PortfolioCheckController',
                        size: 'lg',
                        backdrop: 'static'
                    });

                    modalInstancePortfolio.result.then((out) => {
                        if (out['portfolioCheck'] == true) {
                            ctx.$rootScope['loading'] = true;
                            var promiseUpdatePortfolio: ng.IPromise<{}> = ctx.PortfolioService.updatePortfolios(ctx.portfolioAddUpdate);
                            ctx.$rootScope['loading'] = false;
                            ctx.cancel();
                        }
                        else {
                            $("#portfolioAddSearchInput").focus();
                        }
                    });
                }
            });
            return promiseCount;
        }
       
        public setPrivate = function () {
            var ctx = this;
            ctx.isShared = false;
        }

        public setShared = function () {
            var ctx = this;
            ctx.isShared = true;
        }
    }

    // register controller with News Angular module
    angular.module('Sustainalytics.Portfolio').controller('PortfolioAddController', function ($rootScope: ng.IScope, $scope: ng.IScope, $q: ng.IQService, $location: ng.ILocationService, PortfolioService, PortfolioCompanyService, notificationFactory: Common.INotificationFactory, $modal: Common.IModal, $modalInstance: Common.IModalInstance, filterId: string, companyId: string) {
        var ctrl = new Sustainalytics.Portfolio.PortfolioAddController($rootScope, $scope, $q, $location, PortfolioService, PortfolioCompanyService, notificationFactory, $modal, $modalInstance, filterId, companyId);
        return ctrl;
    });
} 
